package com.example.work_03;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ServiceActivity extends AppCompatActivity {
    private MyBindService.MyBinder binder;
    private ServiceConnection sc=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder=(MyBindService.MyBinder)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service);
        Button btn1=(Button) findViewById(R.id.btn01);
        Button btn2=(Button) findViewById(R.id.btn02);
        Button btn3=(Button) findViewById(R.id.btn03);
        intent=new Intent(this,MyBindService.class);
        btn1.setOnClickListener(onClickListener);
        btn2.setOnClickListener(onClickListener);
        btn3.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn01:
                    bindService(intent,sc, Service.BIND_AUTO_CREATE);
                    break;
                case R.id.btn02:
                    unbindService(sc);
                    break;
                case R.id.btn03:
                    Toast.makeText(ServiceActivity.this
                            , "Serivce的count值为：" + binder.getCount(),Toast.LENGTH_SHORT
                    ).show();
            }
        }
    };
}
